package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.jiubei.entity.ShopGoods;
import com.wmeimob.fastboot.jiubei.dto.SetUpShopGoodsDTO;
import com.wmeimob.fastboot.jiubei.enums.SetUpShopGoodsTypeEnum;
import com.wmeimob.fastboot.util.StringUtils;
import com.wmeimob.fastboot.jiubei.mapper.ShopGoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName ShopGoodsServiceImpl
 * @Description 商品 服务类实现
 * @Author wjt
 * @Date Fri Aug 28 00:08:28 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ShopGoodsServiceImpl implements ShopGoodsService {

    @Resource
    private ShopGoodsMapper shopGoodsMapper;


    @Override
    public List<ShopGoods> list(ShopGoods shopGoods) {
        Example example = new Example(ShopGoods.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(shopGoods.getId())) {
            criteria.andEqualTo("id", shopGoods.getId());
        }
        if (!StringUtils.isEmpty(shopGoods.getType())) {
            criteria.andLike("type", StringUtils.fullFuzzy(shopGoods.getType()));
        }
        if (!StringUtils.isEmpty(shopGoods.getName())) {
            criteria.andLike("name", StringUtils.fullFuzzy(shopGoods.getName()));
        }
        if (!StringUtils.isEmpty(shopGoods.getClassifyId())) {
            criteria.andEqualTo("classifyId", shopGoods.getClassifyId());
        }
        if (!StringUtils.isEmpty(shopGoods.getSpecification())) {
            criteria.andLike("specification", StringUtils.fullFuzzy(shopGoods.getSpecification()));
        }
        if (!StringUtils.isEmpty(shopGoods.getGoodsDescribe())) {
            criteria.andLike("goodsDescribe", StringUtils.fullFuzzy(shopGoods.getGoodsDescribe()));
        }
        if (!StringUtils.isEmpty(shopGoods.getUnit())) {
            criteria.andLike("unit", StringUtils.fullFuzzy(shopGoods.getUnit()));
        }
        if (!StringUtils.isEmpty(shopGoods.getPrice())) {
            criteria.andEqualTo("price", shopGoods.getPrice());
        }
        if (!StringUtils.isEmpty(shopGoods.getSaleNum())) {
            criteria.andEqualTo("saleNum", shopGoods.getSaleNum());
        }
        if (!StringUtils.isEmpty(shopGoods.getStock())) {
            criteria.andEqualTo("stock", shopGoods.getStock());
        }
        if (!StringUtils.isEmpty(shopGoods.getSort())) {
            criteria.andEqualTo("sort", shopGoods.getSort());
        }
        if (!StringUtils.isEmpty(shopGoods.getRichId())) {
            criteria.andEqualTo("richId", shopGoods.getRichId());
        }
        if (!StringUtils.isEmpty(shopGoods.getGrounding())) {
            criteria.andEqualTo("grounding", shopGoods.getGrounding());
        }
        if (!StringUtils.isEmpty(shopGoods.getHot())) {
            criteria.andEqualTo("hot", shopGoods.getHot());
        }
        if (!StringUtils.isEmpty(shopGoods.getDel())) {
            criteria.andEqualTo("del", shopGoods.getDel());
        }
        if (!StringUtils.isEmpty(shopGoods.getGmtCreate())) {
            criteria.andEqualTo("gmtCreate", shopGoods.getGmtCreate());
        }
        if (!StringUtils.isEmpty(shopGoods.getGmtModified())) {
            criteria.andEqualTo("gmtModified", shopGoods.getGmtModified());
        }
        criteria.andEqualTo("del", false);
        example.orderBy("id").desc();
        List<ShopGoods> shopGoodss = shopGoodsMapper.selectByExample(example);
        return shopGoodss;
    }

    @Override
    public ShopGoods queryShopGoodsById(Object id) {
        return shopGoodsMapper.selectByPrimaryKey(id);
    }


    @Override
    public void addShopGoods(ShopGoods shopGoods) {
        shopGoods.setGmtCreate(new Date());
        shopGoodsMapper.insertSelective(shopGoods);
    }

    @Override
    public void removeShopGoods(Object id) {
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setId(Integer.parseInt(id.toString()));
        shopGoods.setDel(true);
        shopGoodsMapper.updateByPrimaryKeySelective(shopGoods);
    }

    @Override
    public void modifyShopGoods(ShopGoods shopGoods) {
        shopGoods.setGmtModified(new Date());
        shopGoodsMapper.updateByPrimaryKeySelective(shopGoods);
    }

    @Override
    public void setUpProportion(SetUpShopGoodsDTO setUpShopGoods) {
        if (StringUtils.isEmpty(setUpShopGoods.getProportion())) {
            throw new CustomException("请输入积分比例");
        }
        if (SetUpShopGoodsTypeEnum.ALL.name().equals(setUpShopGoods.getType())) {
            //设置所有商品积分比例
            SetUpShopGoodsDTO setUpGoods = new SetUpShopGoodsDTO();
            setUpGoods.setProportion(setUpShopGoods.getProportion());
            shopGoodsMapper.updateGoodsProportion(setUpGoods);

        } else if (SetUpShopGoodsTypeEnum.CLASSIFY.name().equals(setUpShopGoods.getType())) {
            SetUpShopGoodsDTO setUpGoods = new SetUpShopGoodsDTO();
            setUpGoods.setProportion(setUpShopGoods.getProportion());
            setUpGoods.setClassifyId(setUpShopGoods.getClassifyId());
            shopGoodsMapper.updateGoodsProportion(setUpGoods);
        } else if (SetUpShopGoodsTypeEnum.SINGLE.name().equals(setUpShopGoods.getType())) {
            if (!ObjectUtils.isEmpty(setUpShopGoods.getProportionGoodsList())) {
                shopGoodsMapper.batchUpdateProportion(setUpShopGoods.getProportionGoodsList());
            }

        }
    }

}
